﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace ScheduleManagement.BLL
{
    /// <summary>
    /// 用户登陆处理类
    /// </summary>
    public class Login_Bll
    {
        ScheduleManagement.DAL.Login_Dal loginDal = new ScheduleManagement.DAL.Login_Dal();
        ScheduleManagement.DAL.User_Dal userDal = new ScheduleManagement.DAL.User_Dal();

        //列出所有的好友信息并根据最后登陆时间排序
        public SqlDataReader List()
        {        
            SqlDataReader dr = loginDal.List();
            return dr;
        }

        //检查用户是否为记住密码
        public string IsRemember(string username)
        {            
            return loginDal.IsRemember(username);
        }


        //验证用户是否合法，并更新登陆信息
        public bool CheckUser(int chkstate)
        {
            if (ScheduleManagement.MODEL.Users.U_Name.Equals(string.Empty) || ScheduleManagement.MODEL.Users.U_Name.Equals(null))
            {
                throw new Exception("用户名不能为空");
            }
            else
            {
                int flag = loginDal.CheckUser();//检查用户是否合法
                if (flag==0) 
                {
                    //如果用户合法则更新登陆信息返回成功
                    loginDal.UpdateLoginInfo(ScheduleManagement.MODEL.Users.U_Name, chkstate);//更新登陆信息
                    return true;
                }
                else if (flag == 1)
                {
                    throw new Exception("用户不存在！");
                }
                else if (flag == 2)
                {
                    throw new Exception("密码不正确！");
                }
                else
                {
                    throw new Exception("遇到未知错误！");
                }
            }
        }

        /// <summary>
        /// 获取用户ID及权限
        /// </summary>
        public void SetUserInfo()
        {
            ScheduleManagement.MODEL.Users.Ch_Id = userDal.CheckUser();
            ScheduleManagement.MODEL.Users.U_Id = loginDal.ReturnUserId();
        }
    }
}
